package com.maxatime.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.maxatime.pojo.entity.Dormitory;
import com.maxatime.pojo.entity.EmployeeInfo;
import com.maxatime.pojo.entity.StudentBasicInfo;
import com.maxatime.pojo.entity.StudentDormitoryRelation;
import com.maxatime.service.DormitoryService;
import com.maxatime.service.StudentBasicInfoService;
import com.maxatime.service.StudentDormitoryRelationSerivce;
import com.maxatime.util.MyContent;

@Controller
@RequestMapping("/ss")
public class DormitoryController {
	// 日志
		Logger log = LogManager.getLogger();
	
	@Autowired
	DormitoryService dormitoryService;
	
	@Autowired
	StudentDormitoryRelationSerivce studentDormitoryRelationSerivce;//中间表
	
	@Autowired
	StudentBasicInfoService studentBasicInfoService;//学生表
	
	/**
	 * 
	 * @Title: cha   
	 * @Description: TODO(学生宿舍管理系统)   
	 * @param: @return      
	 * @return: String      
	 * @throws   
	 * @author: ssb    
	 * @date:   2023年10月30日 下午2:05:09   
	 * @version V1.0
	 */
	@RequestMapping("/cha")
	public String cha() {
		return "dormitory/Dormitory";
	}
	
	//查询
	@RequestMapping("/questData")
	@ResponseBody
	public Map<String, Object> questData(@RequestBody Dormitory dormitory){
//		String area = dormitory.getArea();
//		String buildingNo = dormitory.getBuildingNo();
//		String buildingFloor = dormitory.getBuildingFloor();
//		String dormitoryName = dormitory.getDormitoryName();
//		String dormSupervisor = dormitory.getDormSupervisor();
//		Integer bedNumber = dormitory.getBedNumber();
//		Dormitory dormitory1 = new Dormitory();
//		dormitory1.setArea(area);
//		dormitory1.setBuildingNo(buildingNo);
//		dormitory1.setBuildingFloor(buildingFloor);
//		dormitory1.setDormitoryName(dormitoryName);
//		dormitory1.setDormSupervisor(dormSupervisor);
//		dormitory1.setBedNumber(bedNumber);
		List<Dormitory> list = dormitoryService.listData(dormitory);
		Map<String, Object > m = new HashMap<>();
		m.put(MyContent.CODE, "0000");
		m.put(MyContent.MSG, "查询成功");
		m.put(MyContent.DATA, list);
		return m; 
	}
	
	
	//新增
	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object > save(@RequestBody Dormitory dormitory){
		Map<String , Object> m = new HashMap<>();
		dormitoryService.insertSelective(dormitory);
		m.put(MyContent.CODE, "11111");
		m.put(MyContent.MSG, "新增成功");
		
		return m;
	}
	
	//查询管理员
	@RequestMapping("/gly")
	@ResponseBody
	public Map<String, Object> gly(@RequestBody Dormitory dormitory){
		Map<String, Object> m = new HashMap<>();
		EmployeeInfo employeeInfo=new EmployeeInfo();//对象
		List<EmployeeInfo> list = dormitoryService.gly(employeeInfo);
		 m.put(MyContent.CODE, "0000");
		 m.put(MyContent.MSG, "交易成功");
		 m.put(MyContent.DATA, list);
		return m;
	}
	 
	//删除
	@RequestMapping("/del")
	@ResponseBody
	public Map<String, Object> del(@RequestBody String jsonStr){
		JSONObject jsonObj = JSONObject.parseObject(jsonStr);
		String pkId = jsonObj.getString("pkId");
		dormitoryService.deleteByPrimaryKey(Integer.parseInt(pkId));
		
		Map<String, Object> m = new HashMap<>();
		m.put(MyContent.CODE, "2222");
		m.put(MyContent.MSG, "删除成功");
		return m;
		
	}
	
	//反显
	@RequestMapping("/findPkId")
	@ResponseBody
	public Map<String, Object> findPkId(@RequestBody String jsonStr){
		JSONObject jsonObj = JSONObject.parseObject(jsonStr);
		String pkId = jsonObj.getString("pkId");
		Dormitory dormitory=dormitoryService.findpkId(Integer.parseInt(pkId));
		Map<String, Object> m = new HashMap<>();
		m.put(MyContent.CODE, "33333");
		m.put(MyContent.MSG, "反显!");
		m.put(MyContent.DATA, dormitory);
		return m;
	}
	
	//修改
	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> update(@RequestBody String jsonStr){
		JSONObject jsonObj = JSONObject.parseObject(jsonStr);
		String pkId = jsonObj.getString("pkId");
		String area = jsonObj.getString("area");
		String buildingNo = jsonObj.getString("buildingNo");
		String buildingFloor = jsonObj.getString("buildingFloor");
		String dormitoryName = jsonObj.getString("dormitoryName");
		String bedNumber = jsonObj.getString("bedNumber");
		String dormSupervisor = jsonObj.getString("dormSupervisor");
		String useStatus = jsonObj.getString("useStatus");
		Dormitory dormitory = new Dormitory();
		dormitory.setPkId(Integer.parseInt(pkId));
		dormitory.setArea(area);
		dormitory.setBuildingNo(buildingNo);
		dormitory.setBuildingFloor(buildingFloor);
		dormitory.setDormitoryName(dormitoryName);
		dormitory.setBedNumber(Integer.parseInt(bedNumber)); 
		dormitory.setDormSupervisor(dormSupervisor);
		dormitory.setUseStatus(useStatus);
		dormitoryService.updateByPrimaryKeySelective(dormitory);
		Map<String, Object> m = new HashMap<>();
		m.put(MyContent.CODE, "4444");
		m.put(MyContent.MSG, "修改!");
		m.put(MyContent.DATA, dormitory);
		return m;
	}
	
	//添加学生跳转
	@RequestMapping("/studenttz")
	public String studenttz(@RequestParam String pkId, Model model) {
		Dormitory dormitory = new Dormitory();
		dormitory = dormitoryService.select(Integer.parseInt(pkId));
		model.addAttribute("Dormitory", dormitory);
		return "dormitory/xiangqingxs";
	}
	//添加学生模态框查询
	@RequestMapping("/questudent")
	@ResponseBody
	public Map<String , Object> questudent(@RequestBody String jsonStr){
		   JSONObject jsonObject = JSONObject.parseObject(jsonStr);
		   Map<String, Object > m = new HashMap<>();
		   String dormitoryId = jsonObject.getString("dormitoryId");
		   
		   List<StudentDormitoryRelation> list = studentDormitoryRelationSerivce.listData(dormitoryId);//1.把查到的几条数据赋值给list
		   
		   StudentBasicInfo studentBasicInfo=new  StudentBasicInfo();
		   List<StudentBasicInfo> studentBasicInfo1=new ArrayList<StudentBasicInfo>();
		   
		   
		   for(int i = 0 ; i<list.size() ; i ++) {
			   studentBasicInfo = studentDormitoryRelationSerivce.listData2(list.get(i).getStudentId());//2.把数值一条一条拿出来，给对象studentBasicInfo1
			   
			   studentBasicInfo1.add(studentBasicInfo);
			  
			  
			  m.put(MyContent.DATA, studentBasicInfo1);
		   }
		   
		   m.put(MyContent.CODE, "5555");
		   m.put(MyContent.MSG, "查询成功");
		return m;
	}
	
	//添加学生删除
	@RequestMapping("/delstudent")
	@ResponseBody
	public Map<String, Object> delstudent(@RequestBody String jsonStr){
		JSONObject jsonObject = JSONObject.parseObject(jsonStr);
		Map<String, Object > m = new HashMap<>();
		String studentId = jsonObject.getString("studentId");
		studentDormitoryRelationSerivce.deleteByPrimaryKey(studentId);
		 m.put(MyContent.CODE, "6666");
		 m.put(MyContent.MSG, "删除成功");
		return m;
	}
	
	//添加学生查询
	@RequestMapping("/insert")
	@ResponseBody
	public Map<String, Object> insert(@RequestBody StudentBasicInfo studentBasicInfo){
		
		Map<String, Object> m = new HashMap<>();
		List<StudentBasicInfo> list = studentDormitoryRelationSerivce.listData1(studentBasicInfo);
		 m.put(MyContent.CODE, "6666");
		  m.put(MyContent.MSG, "查询学生成功");
		  m.put(MyContent.DATA, list);
		return m;
	}
	//添加学生
	@RequestMapping("/insertstu")
	@ResponseBody
	public Map<String, Object> insertstu(@RequestBody StudentDormitoryRelation studentDormitoryRelation){
		Map<String, Object> m = new HashMap<>();
		studentDormitoryRelationSerivce.insertSelective(studentDormitoryRelation);
		m.put(MyContent.CODE, "7777");
		m.put(MyContent.MSG, "新增学生成功");
		return m;
	}
}
